package blocksim.hash;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Adapter for the MessageDigest hash implementations
 * 
 * @author marcus
 * 
 */
public class MessageDigestHashStrategy extends AbstractBufferedHashStrategy {

  private MessageDigest md;
  private final String name;

  public MessageDigestHashStrategy(int windowSize, String algorithm)
      throws NoSuchAlgorithmException {
    super(windowSize);
    this.name = algorithm;
    this.md = MessageDigest.getInstance(algorithm);
  }

  @Override
  public byte[] hash() {
    byte[] array = data.array();
    return md.digest(array);
  }

  @Override
  public String getName() {
    return name;
  }
}
